[#648] Add disable_ssl_verification option to WebRequestConcern

This allows the user to disable Faraday's verification of the site's SSL
certificate.

Vitor Baptista 10 years ago
parent
commit
41c8ee7a35
2 changed files with 37 additions and 1 deletions
  1. 11 1
      app/concerns/web_request_concern.rb
  2. 26 0
      spec/concerns/web_request_concern_spec.rb

+ 11 - 1
app/concerns/web_request_concern.rb

@@ -9,6 +9,10 @@ module WebRequestConcern
9 9
       errors.add(:base, "user_agent must be a string") unless options['user_agent'].is_a?(String)
10 10
     end
11 11
 
12
+    if options['disable_ssl_verification'].present? and not [true, false].include? options['disable_ssl_verification']
13
+      errors.add(:base, "if provided, disable_ssl_verification must be a boolean")
14
+    end
15
+
12 16
     unless headers(options['headers']).is_a?(Hash)
13 17
       errors.add(:base, "if provided, headers must be a hash")
14 18
     end
@@ -21,7 +25,13 @@ module WebRequestConcern
21 25
   end
22 26
 
23 27
   def faraday
24
-    @faraday ||= Faraday.new { |builder|
28
+    faraday_options = {
29
+      ssl: {
30
+        verify: !options['disable_ssl_verification']
31
+      }
32
+    }
33
+
34
+    @faraday ||= Faraday.new(faraday_options) { |builder|
25 35
       builder.headers = headers if headers.length > 0
26 36
 
27 37
       builder.headers[:user_agent] = user_agent

+ 26 - 0
spec/concerns/web_request_concern_spec.rb

@@ -19,6 +19,18 @@ describe WebRequestConcern do
19 19
       faraday = web_request.faraday
20 20
       expect(faraday.builder.handlers).to include(FaradayMiddleware::FollowRedirects)
21 21
     end
22
+
23
+    it 'should enable SSL verification by default' do
24
+      web_request = WebRequestConcernTest.new()
25
+      faraday = web_request.faraday
26
+      expect(faraday.ssl.verify).to eq(true)
27
+    end
28
+
29
+    it 'should disable SSL verification if disable_ssl_verification option is true' do
30
+      web_request = WebRequestConcernTest.new(options: { disable_ssl_verification: true })
31
+      faraday = web_request.faraday
32
+      expect(faraday.ssl.verify).to eq(false)
33
+    end
22 34
   end
23 35
 
24 36
   describe '#validate_web_request_options!' do
@@ -71,5 +83,19 @@ describe WebRequestConcern do
71 83
         expect(web_request.errors[:base]).to_not be_empty
72 84
       end
73 85
     end
86
+
87
+    describe 'disable_ssl_verification' do
88
+      it 'should be a boolean' do
89
+        web_request = WebRequestConcernTest.new(options: { disable_ssl_verification: true } )
90
+        web_request.validate_web_request_options!
91
+        expect(web_request.errors[:base]).to be_empty
92
+      end
93
+
94
+      it 'should be invalid if not a boolean' do
95
+        web_request = WebRequestConcernTest.new(options: { disable_ssl_verification: 42 } )
96
+        web_request.validate_web_request_options!
97
+        expect(web_request.errors[:base]).to_not be_empty
98
+      end
99
+    end
74 100
   end
75 101
 end